package com.ktwlsoft.archivemanage.domain.business.businessService.study;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.study.StudyClassContentRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.study.StudyClassifyRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.study.StudyClassRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.study.StudyClass;
import com.ktwlsoft.archivemanage.domain.dao.entity.study.StudyClassContent;
import com.ktwlsoft.archivemanage.domain.dao.entity.study.StudyClassify;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.study.studyClass.ClassManagePageRequest;
import com.ktwlsoft.archivemanage.payload.study.AddClassifyRequest;
import com.ktwlsoft.archivemanage.payload.study.UpdateClassifyRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 指南字典
 *
 * @author Joker
 * @version 1.0
 */
@Component
public class StuduClassifyService {

    /**
     * 课程分类
     */
    private final StudyClassifyRepositoryService studyClassifyRepositoryService;
    private final StudyClassRepositoryService studyClassRepositoryService;
    /**
     * 课程内容
     */
    private final StudyClassContentRepositoryService studyClassContentRepositoryService;


    public StuduClassifyService(StudyClassifyRepositoryService studyClassifyRepositoryService, StudyClassRepositoryService studyClassRepositoryService, StudyClassContentRepositoryService studyClassContentRepositoryService) {
        this.studyClassifyRepositoryService = studyClassifyRepositoryService;
        this.studyClassRepositoryService = studyClassRepositoryService;
        this.studyClassContentRepositoryService = studyClassContentRepositoryService;
    }

    /**
     * 课程分类列表
     *
     * @return Result<?>
     */
    public Result<?> list(StudyClassify.ClassType classType) {
        List<StudyClassify> classifyList = studyClassifyRepositoryService.getStudyClassifyMapper().selectByClassType(classType);
        return Result.ok(classifyList);
    }

    /**
     * 课程分类新增
     *
     * @param request request
     * @return Result<?>
     */
    public Result<?> insert(AddClassifyRequest request) {
        if (request.getLevel() > 3) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("分类层级不能大于3");
        } else {
            StudyClassify studyClassify = new StudyClassify();
            BeanUtils.copyProperties(request, studyClassify);
            try {
                studyClassifyRepositoryService.getStudyClassifyMapper().insert(studyClassify);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.body(null, ResultType.SAVING_ERROR);
            }
            return Result.ok(studyClassify.getId());
        }
    }

    /**
     * 课程分类修改
     *
     * @param request request
     * @return Result<?>
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> update(UpdateClassifyRequest request) {
        if (request.getLevel() > 3) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("分类层级不能大于3");
        } else {
            StudyClassify studyClassify = studyClassifyRepositoryService.getStudyClassifyMapper().selectByPrimaryKey(request.getId());
            if (studyClassify == null) {
                return Result.body(null, ResultType.CHECK_ERROR).withMessage("该数据不存在");
            }
            studyClassify.setName(request.getName());
            studyClassify.setLevel(request.getLevel());
            studyClassify.setParentId(request.getParentId());
            studyClassify.setClassType(request.getClassType());
            try {
                studyClassifyRepositoryService.getStudyClassifyMapper().updateByPrimaryKey(studyClassify);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.body(null, ResultType.UPDATE_ERROR);
            }
            return Result.ok(studyClassify.getId());
        }
    }

    /**
     * 课程分类删除
     *
     * @param id id
     * @return Result<?>
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> deleteById(Long id) {
        if (id == null || id <= 0) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("id不能为空");
        }
        StudyClassify studyClassify = studyClassifyRepositoryService.getStudyClassifyMapper().selectByPrimaryKey(id);
        if (studyClassify == null) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("该数据不存在");
        }
        if (studyClassify.getId() != null) {
            //是否有引用
            List<StudyClass> studyClasses = studyClassRepositoryService.getStudyClassMapper().selectByClassifyId(id);
            if (studyClasses != null && studyClasses.size() > 0) {
                return Result.body(null, ResultType.CHECK_ERROR).withMessage("无法删除,该数据已被引用");
            }
        }
        try {
            studyClassifyRepositoryService.getStudyClassifyMapper().deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.body(null, ResultType.DELETE_ERROR);
        }
        return Result.ok(studyClassify.getId());
    }

    /**
     * 课程管理分页
     *
     * @param request
     * @param request
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<StudyClass> classManagePage(ClassManagePageRequest request) {
        PageInfo<StudyClass> pageInfo = studyClassRepositoryService.classManagePage(request);
        return Result.ok(pageInfo);
    }

    /**
     * 专题课程列表
     *
     * @param subjectId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<StudyClass> selectBySubjectId(Integer pn, Integer pageSize, Long subjectId, String classType) {
        PageInfo<StudyClass> pageInfo = studyClassRepositoryService.selectBySubjectId(pn, pageSize, subjectId, classType);
        if (pageInfo.getList() != null && pageInfo.getList().size() > 0) {
            List<StudyClassContent> contentlist = studyClassContentRepositoryService.selectAll();
            List list = Lists.newArrayList();
            for (StudyClass studyClass : pageInfo.getList()) {
                JSONObject object = new JSONObject();
                object.put("id", studyClass.getId());
                object.put("title", studyClass.getTitle());
                object.put("mainImgUrl", studyClass.getMainImgUrl());
                object.put("num", getNum(contentlist, studyClass));
                object.put("creator", studyClass.getPublisherName());
                list.add(object);
            }
            pageInfo.setList(list);
        }
        return Result.ok(pageInfo);
    }

    /**
     * 获取课程组的数量
     *
     * @param contentlist
     * @param value
     * @return
     */
    private int getNum(List<StudyClassContent> contentlist, StudyClass value) {
        int i = 0;
        if (contentlist != null && contentlist.size() > 0) {
            for (StudyClassContent content : contentlist) {
                if (content.getClassId().equals(value.getId())) {
                    i++;
                }
            }
        }
        return i;
    }
}


